/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backingbeans;

import entities.UUser;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import sessions.UserManager;
import utils.ClientMessage;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@ManagedBean(name = "friendBean")
@ViewScoped
public class FriendBean implements Serializable {

    @EJB
    private UserManager userManager;
    private UUser selectedFriend;
    @Inject
    private UserSession userSession;
    private Converter userConverter = new Converter() {
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            try {
                return userManager.getUser(Long.valueOf(value));
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value == null) {
                return "";
            }

            return String.valueOf(((UUser) value).getId());
        }
    };

    public UUser getSelectedFriend() {
        return selectedFriend;
    }

    public void setSelectedFriend(UUser selectedFriend) {
        this.selectedFriend = selectedFriend;
    }

    public Converter getUserConverter() {
        return userConverter;
    }

    public void setUserConverter(Converter userConverter) {
        this.userConverter = userConverter;
    }

    public List<UUser> autocomplete(String query) {
        // Get matching users
        List<UUser> users = userManager.getUserLike(userSession.getUser().getId(), query);
        return users;
    }

    public void addFriend() {
        if (!userSession.getUser().isFriendWith(selectedFriend)) {
            if (userManager.addFriend(userSession.getUser(), selectedFriend)) {
                ClientMessage.sendClientMessage("Ami ajouté !", FacesMessage.SEVERITY_INFO);
            } else {
                ClientMessage.sendClientMessage("Une erreur s'est produite !", FacesMessage.SEVERITY_ERROR);
            }
        }
        else {
            ClientMessage.sendClientMessage("Vous-êtes déjà ami avec cette personne.", FacesMessage.SEVERITY_ERROR);
        }
    }

    public void removeFriend() {
        if (userManager.removeFriend(userSession.getUser(), selectedFriend)) {
            ClientMessage.sendClientMessage("Ami retiré de votre liste !", FacesMessage.SEVERITY_INFO);
        } else {
            ClientMessage.sendClientMessage("Vous êtes déjà ami avec cette personne !", FacesMessage.SEVERITY_ERROR);
        }
    }
}
